const App = getApp();
import siteinfo from '../../siteinfo.js';
const apiroot = siteinfo.siteroot + 'index.php?s=/api/';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    optionList: ['所有', '选项1', '选项2'],
    value: '所有',
    phone:'',
    modal1: false,
    hideFlag: true,//true-隐藏  false-显示
    hideFlag11: true,
    animationData: {},//
    show: true,
    modal1: false,
    hideFlag1: true,//true-隐藏  false-显示
    hideFlag111: true,
    animationData1: {},//
    show1: true,
    carNo: '',
    translateSpace: 0,
    inputType: 1, // 车牌输入类型，1简称，2数字或者字母,
    showPlateInput: false,
    title:'',
    content:'',
    shop_name:'',
    id:'',
    shop_phone:'',
    lat:'',
    lng:'',
    man:false,
    goods_id:'',
    ceshi:false,
    src:'',
    idd:'',
    openidas:'',
    showdata: [1,2,3],
    marginTop: 0,
    showLine: 0,
    lineHeight:60,
    animationScroll:500,
    animation:500,
    showNum:5,
    data:[],
    msgList: [
      { title: "朋友圈" },
      { title: "文章" },
      { title: "公共号" },
      { title: "小程序" },
      { title: "音乐" },
      { title: "表情" },
      { title: "订阅号" }
    ],
    bao: true,
    num:0
  },

  onLoad: function (e) {
    this.init()
    
    var that = this
    var scene = App.getSceneData(e);
    that.data.iid = e.id ? e.id : scene.id;
    that.setData({
      openidas:scene
    })
    console.log(scene)
    if (!wx.getStorageSync('user_id')){
  App.doLogin()
}
    wx.request({
      url: apiroot+'manba/acavt', //仅为示例，并非真实的接口地址
      data: {
        id:e.id
      },

      success(res) {
        console.log(res.data)
        that.setData({
          num: res.data.result.num,
          xinxi: res.data.result.shuzu,
          shuzu: res.data.result.bei_img,
          title: res.data.result.title,
          shop_name: res.data.result.shop_name,
          id: res.data.result.id,
          shop_phone: res.data.result.shop_phone,
          lat: res.data.result.lat,
          lng: res.data.result.lng,
          content: res.data.result.content,
          idd: res.data.result.id,
          goods_id: res.data.result.goods_id,
          deduct_stock_type:res.data.result.deduct_stock_type
        })
        console.log(res.data)
      }
    })

    wx.request({
      url: apiroot + 'manba/lis', //仅为示例，并非真实的接口地址
      data: {
        id: e.id,
        user_id: wx.getStorageSync('user_id'),
      },

      success(res) {
        console.log(res.data)
        if (res.data.code==200){
          that.setData({
            bao: false,
            man:true
          })
         
        }
        if (res.data.code == 100) {
          that.setData({
              bao: true,
               man: false
          })

        }
      }
    })
    // wx.showLoading({
    //   mask: false,
    //   title: '加载中',
    // })

    // setTimeout(function () {
    //   wx.hideLoading()
    // }, 1000)
    // wx.showModal({
    //   title: '提示',
    //   content: '这是一个模态弹窗',
    //   success(res) {
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //     } else if (res.cancel) {
    //       console.log('用户点击取消')
    //     }
    //   }
    // })


  },

  init: function () {
    console.log(44444);
    this.data.showLine = this.data.showNum < this.data.length ? this.data.showNum : this.data.length;
    for (let i = 0; i < this.data.length; i++) {
      this.data.showdata.push(this.data.data[i]);
    }

    for (let i = 0; i < this.data.showLine; i++) {
      this.data.showdata.push(this.data[i]);
    }
    setInterval(this.animationFunc, this.data.animation);
  },
  animationFunc: function () {
    if (this.data.marginTop >= this.data.length * this.data.lineHeight) {
      this.data.marginTop = 0;
    }
    let stepTime = this.data.animationScroll / this.data.lineHeight;

    var step = 0;
    let self = this;
    var index = setInterval(function () {
      self.data.marginTop = self.data.marginTop + 1;
      step++;
      if (step >= self.lineHeight) {
        clearInterval(index);
      }
    }, stepTime);
  },
  dianhua(e) {

    this.setData({
      phone: e.detail.value
    })
  },
  baoming(){
    var manba = this
    if (manba.data.phone == '') {
      wx.showToast({
        title: '电话不能为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if (manba.data.carNo == '') {
      wx.showToast({
        title: '车牌不能为空',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: apiroot + 'manba/activity', //仅为示例，并非真实的接口地址
      data: {
        user_id: wx.getStorageSync('user_id'),
        phone: manba.data.phone,
        plate: manba.data.carNo,
        activity_id: manba.data.idd
      },

      success(res) {
       
        if (res.data.code==200){
          wx.showToast({
            title: '报名成功',
            icon: 'none',
            duration: 2000
          })
         manba.hideModal()
          manba.setData({
          man :true
          })
         
}else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          return false;
}
      }
    })
  },
  goumai(){
var manba=this
    if(manba.data.man==false){
manba.showModal()
return false;
    }
    if (manba.data.deduct_stock_type==10){
      wx.showToast({
        title: '此活动只能报名',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    wx.navigateTo({
      url: '/pages/goods/index?goods_id=' + manba.data.goods_id+'&manba='+1,
    })


   
  },
call(){
  wx.makePhoneCall({

    phoneNumber: this.data.shop_phone,

  })
},

  onOpenLocation() {
    let _this = this,
      detail = _this.data.detail;
    wx.openLocation({
      name: _this.data.shop_name,
      address: '',
      longitude: Number(_this.data.lng),
      latitude: Number(_this.data.lat),
      scale: 15
    });
  },
  inToggleModal1(e) {
    console.log(e);
    this.setData({
      show: !this.data.show
    });
  },
  inCancel(e) {
    console.log(e);
    this.setData({
      show: !this.data.show
    });
  },
  inSuccess(e) {
    console.log(e);
    this.setData({
      show: !this.data.show
    });
  },
  // 点击选项
  getOption: function (e) {
    var that = this;
    that.setData({
      value: e.currentTarget.dataset.value,
      hideFlag: true
    })
  },
  post11(){

var man=this;
man.setData({
  ceshi:false
})
  },
  previewImage(e){
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current,
      urls: [current]
    })
  },
  shaee(){
var that=this
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: 'https://weicheng.6sjs.com/index.php?s=/api/manba/ma', //仅为示例，并非真实的接口地址
      data: {
        id: that.data.id,
        token: wx.getStorageSync('token'),
        user_id: wx.getStorageSync('user_id'),
        img:that.data.shuzu[0],
        title: that.data.title,
        wxapp_id: App.getWxappId()
      },

      success(res) {
        
        console.log(res.data)
        if (res.data.code==200){
          wx.hideLoading();
          that.hideModal1();
          that.setData({
            src: res.data.result,
            ceshi:true
          })
        }
      }
    })
  },
  //取消
  mCancel: function () {
    var that = this;
    that.hideModal();
  },
  mCancel1: function () {
    var that = this;
    that.hideModal1();
  },

  // ----------------------------------------------------------------------modal
  // 显示遮罩层
  showModal: function () {
    var that = this;
    that.setData({
      hideFlag: false
    })
    // 创建动画实例
    var animation = wx.createAnimation({
      duration: 100,//动画的持续时间
      timingFunction: 'linear',//动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
    })
    this.animation = animation; //将animation变量赋值给当前动画
    var time1 = setTimeout(function () {
      that.slideIn();//调用动画--滑入
      clearTimeout(time1);
      time1 = null;
    }, 50)
  },

  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 100,//动画的持续时间 默认400ms
      timingFunction: 'linear',//动画的效果 默认值是linear
    })
    this.animation = animation
    that.slideDown();//调用动画--滑出
    var time1 = setTimeout(function () {
      that.setData({
        hideFlag: true
      })
      clearTimeout(time1);
      time1 = null;
    }, 50)//先执行下滑动画，再隐藏模块

  },
  //动画 -- 滑入
  slideIn: function () {
    this.animation.translateY(0).step() // 在y轴偏移，然后用step()完成一个动画
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData: this.animation.export()
    })
  },
  //动画 -- 滑出
  slideDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },


  // ----------------------------------------------------------------------modal
  // 显示遮罩层
  showModal1: function () {
    var that = this;
    that.setData({
      hideFlag1: false
    })
    // 创建动画实例
    var animation1 = wx.createAnimation({
      duration: 100,//动画的持续时间
      timingFunction: 'linear',//动画的效果 默认值是linear->匀速，ease->动画以低速开始，然后加快，在结束前变慢
    })
    this.animation1 = animation1; //将animation变量赋值给当前动画
    var time1 = setTimeout(function () {
      that.slideIn1();//调用动画--滑入
      clearTimeout(time1);
      time1 = null;
    }, 50)
  },

  // 隐藏遮罩层
  hideModal1: function () {
    var that = this;
    var animation1 = wx.createAnimation({
      duration: 100,//动画的持续时间 默认400ms
      timingFunction: 'linear',//动画的效果 默认值是linear
    })
    this.animation1 = animation1
    that.slideDown1();//调用动画--滑出
    var time1 = setTimeout(function () {
      that.setData({
        hideFlag1: true
      })
      clearTimeout(time1);
      time1 = null;
    }, 50)//先执行下滑动画，再隐藏模块

  },
  //动画 -- 滑入
  slideIn1: function () {
    this.animation1.translateY(0).step() // 在y轴偏移，然后用step()完成一个动画
    this.setData({
      //动画实例的export方法导出动画数据传递给组件的animation属性
      animationData1: this.animation1.export()
    })
  },
  //动画 -- 滑出
  slideDown1: function () {
    this.animation1.translateY(300).step()
    this.setData({
      animationData1: this.animation1.export(),
    })
  },


  /* 用于点击弹出键盘输入，space为键盘弹出后向上拉取的距离 */
  handleClick(e) {
    /* 150为键盘的高度 */
    let space = -(e.currentTarget.offsetTop - 150);
    /* regExp用于判断当前已输入的车牌号是否是中文，并让键盘显示中文还是英文输入 */
    let regExp = /^[\u4e00-\u9fa5]+/;
    let inputType = 1;
    if (regExp.test(this.data.carNo)) {
      inputType = 2;
    }

    this.setData({
      translateSpace: space,
      showPlateInput: true,
      inputType
    })
  },
  /* 键盘输入操作 */
  handlePlateChange(e) {
    let value = e.detail.value;
    let type = e.detail.type;
    let carNo = this.data.carNo;
    carNo += value;

    if (type == 1) {
      this.setData({
        inputType: 2
      })
    }
    this.setData({
      carNo
    })
  },
  /* 点击键盘上的确定 */
  handlePlateConfirm() {
    /* isCarPlate用于判断输入的车牌号是否符合规范 */
    if (!this.isCarPlate(this.data.carNo)) {
      wx.showToast({
        title: '请输入正确的车牌号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      translateSpace: 0,
      showPlateInput: false,
      inputType: 1
    })
  },
  /* 用于键盘输入删除 */
  handlePlateDelete(e) {
    let carNo = this.data.carNo;
    carNo = carNo.substring(0, carNo.length - 1);
    if (carNo.length == 0) {
      this.setData({
        inputType: 1
      })
    }
    this.setData({
      carNo,
    })
  },
  handlePlateGuan(e) {
    console.log(33333)
    this.setData({
      translateSpace: 0,
      showPlateInput: false,
      inputType: 1
    })
  },
  /* 判断车牌号 */
  isCarPlate(value) {
    return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(value);
  },


  onShareAppMessage() {
    const _this = this;
    // 构建页面参数
    const params = App.getShareUrlParams({
      'id': _this.data.idd
    });
    return {
      title: _this.data.title,
      path: "/pages/avact/index?id=" + _this.data.idd,
      imageUrl: _this.data.shuzu[0], 
    };
  },

})
